﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.SessionState;

namespace HQWeb.View
{
    /// <summary>
    /// GetSearchItem 各个展示模块的查询条件
    /// </summary>
    public class GetSearchItem : IHttpHandler, IReadOnlySessionState
    {
        private static string serachSearch1="";
        private static string serachSearch2="";
        private static string serachSearch3="";
        private static string serachSearch4="";
        private static string serachSearch5="";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Session["BRANCH_ID"] != null)
            {
                string strType = context.Request["type"];
                StringBuilder strHtml = new StringBuilder();
                strHtml.Append("  <div class=\"navlist\">");
                strHtml.Append("   <h3>");

                strHtml.Append(" <a target=\"_blank\" onclick=\"CleanSearch()\" style=\"cursor:pointer\"> 显示全部 </a>");
                strHtml.Append("    </h3>");
                strHtml.Append(" </div>");
                strHtml.Append("  <div class=\"navlist\">");
                strHtml.Append("   <h3>");

                strHtml.Append(" <a target=\"_blank\" onclick=\"OpenMatrial()\" style=\"cursor:pointer\"> 推荐物品 </a>");
                strHtml.Append("    </h3>");
                strHtml.Append(" </div>");
                if (strType == "1")
                {
                    serachSearch2 = serachSearch2 == "" ? GerSearch2() : serachSearch2;
                    context.Response.Write(serachSearch2 + strHtml.ToString());
                }
                else if (strType == "2")
                {
                    serachSearch1 = serachSearch1 == "" ? GerSearch1() : serachSearch1;
                    context.Response.Write(serachSearch1 + strHtml.ToString());
                }
                else if (strType == "3")
                {
                    serachSearch3 = serachSearch3 == "" ? GerSearch3() : serachSearch3;
                    context.Response.Write(serachSearch3 + strHtml.ToString());
                }
                else if (strType == "4")
                {
                    int strBranchID = Convert.ToInt32(context.Session["BRANCH_ID"].ToString());
                    serachSearch4 = serachSearch4 == "" ? GerSearch4(strBranchID) : serachSearch4;
                    context.Response.Write(serachSearch4 + strHtml.ToString());
                }
                else if (strType == "6")
                {
                    int strBranchID = Convert.ToInt32(context.Session["BRANCH_ID"].ToString());
                    serachSearch5 = serachSearch5 == "" ? GerSearch5(strBranchID) : serachSearch5;
                    context.Response.Write(serachSearch5 + strHtml.ToString());
                }
            }
            else
            {
                context.Response.Write("SessionTimeout");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string GerSearch1()
        {
           
           // strResout = GetNode("1");
            StringBuilder strHtml = new StringBuilder();

            strHtml.Append("  <div class=\"navlist\" onclick='SearchMosOn(this)' style=\"cursor:pointer\" >");
                strHtml.Append("   <h3>");
             
                strHtml.Append("所属模块");
                strHtml.Append("    </h3>");
                strHtml.Append("       <div class=\"navmore\"><dl><dd>");
                strHtml.Append(string.Format(" <a target=\"_blank\" title=\"{0}\"", "婚礼知识"));
                strHtml.Append(string.Format("onclick=\"getSearchDate('{0}','{1}',this)\">{2}</a>", "NEWSCHOOL", "1", "婚礼知识"));
                strHtml.Append(string.Format(" <a target=\"_blank\" title=\"{0}\"", "新人问答"));
                strHtml.Append(string.Format("onclick=\"getSearchDate('{0}','{1}',this)\">{2}</a>", "NEWSCHOOL", "2", "新人问答"));
                strHtml.Append(string.Format(" <a target=\"_blank\" title=\"{0}\"", "本地知识"));
                strHtml.Append(string.Format("onclick=\"getSearchDate('{0}','{1}',this)\">{2}</a>", "NEWSCHOOL", "3", "本地知识"));
                strHtml.Append(" </dd></dl></div></div>");
         
            return strHtml.ToString();
           
        }
        private string GerSearch2()
        {
            string strResout = "";
            strResout = GetNode("1", "喜来展示--");
            strResout = strResout+"  <div class=\"navlist\">";
            strResout = strResout + ("   <h3>");

            strResout = strResout + (" <a target=\"_blank\" onclick=\"getSearchDate('isnull','null')\" style=\"cursor:pointer\"> 未设置 </a>");
            strResout = strResout + ("    </h3>");
            strResout = strResout + (" </div>");
            strResout = strResout + ("  <div class=\"navlist\">");
            strResout = strResout + ("   <h3>");
            return strResout;
        }
        private string GerSearch3()
        {
            string strResout = "";
            strResout = GetNode("3", "婚礼定制--");
            return strResout;
        }
        private string GerSearch4(int strBranchID)
        {
            StringBuilder strHtml = new StringBuilder();
            BLL.SysManage.T_PUB_HOTEL HotelBll = new BLL.SysManage.T_PUB_HOTEL();
            List<Model.SysManage.T_PUB_HOTEL> hotelList = HotelBll.GetModelList(" BRANCH_ID='" + strBranchID + "'");


            strHtml.Append("  <div class=\"navlist\" onclick='SearchMosOn(this)' style=\"cursor:pointer\">");
            strHtml.Append("   <h3>");

            strHtml.Append("酒店名称");
            strHtml.Append("    </h3>");
            strHtml.Append("       <div class=\"navmore\"><dl><dd>");
            foreach (Model.SysManage.T_PUB_HOTEL item in hotelList)
            {
                 strHtml.Append(string.Format(" <a target=\"_blank\" title=\"{0}\"", item.HOTEL_NAME));
                strHtml.Append(string.Format("onclick=\"getSearchDate('{0}','{1}',this)\">{2}</a>", "HOTEL_NAME", item.HOTEL_NAME, item.HOTEL_NAME));
            }
            strHtml.Append(" </dd></dl></div></div>");
           strHtml.Append(GetNode("4",""));
           return strHtml.ToString();
        }
        private string GerSearch5(int strBranchID)
        {
            StringBuilder strResout = new StringBuilder();
            BLL.SysManage.T_PUB_HOTEL HotelBll = new BLL.SysManage.T_PUB_HOTEL();
            List<Model.SysManage.T_PUB_HOTEL> hotelList = HotelBll.GetModelList(" BRANCH_ID='" + strBranchID + "'");
   
            strResout.Append("  <div class=\"navlist\" onclick='SearchMosOn(this)' style=\"cursor:pointer\">");
            strResout.Append("   <h3>");

            strResout.Append("酒店名称");
            strResout.Append("    </h3>");
            strResout.Append("       <div class=\"navmore\"><dl><dd>");
            foreach (Model.SysManage.T_PUB_HOTEL item in hotelList)
            {
                strResout.Append(string.Format(" <a target=\"_blank\" title=\"{0}\"", item.HOTEL_NAME));
                strResout.Append(string.Format("onclick=\"getSearchDate('{0}','{1}',this)\">{2}</a>", "HOTEL_NAME", item.HOTEL_NAME, item.HOTEL_NAME));
            }
            strResout.Append(" </dd></dl></div></div>");
            strResout.Append(GetNode("6", "喜来展示--"));
            return strResout.ToString();
        }

        private string GetNode(string strType,string strReplace)
        {
            StringBuilder strHtml = new StringBuilder();

            BLL.SysManage.T_PUB_DICTIONARY dictBll = new BLL.SysManage.T_PUB_DICTIONARY();
            List<Model.SysManage.T_PUB_DICTIONARY> modelList = dictBll.GetListArray(" FLAG = 1");
            string strItemName = "";
            IEnumerable<Model.SysManage.T_PUB_DICTIONARY> ModelList3 ;
            ModelList3 = from n in modelList where n.PARENT_ID ==0 && n.Type.Contains(strType) select n;
            foreach (Model.SysManage.T_PUB_DICTIONARY item in ModelList3)
            {
                strHtml.Append("  <div class=\"navlist\" onclick='SearchMosOn(this)' style=\"cursor:pointer\">");
                strHtml.Append("   <h3>");
                //strItemName = item.NAME.Replace("喜来展示--", "");
                //strItemName = strItemName.Replace("喜来订制--", "");
                //strItemName = strItemName.Replace("酒店--","");
                strItemName = item.NAME;
                if (strReplace!="")
                {
                    strItemName = strItemName.Replace(strReplace, ""); 
                }
                strHtml.Append(strItemName);
                strHtml.Append("    </h3>");
                strHtml.Append("       <div class=\"navmore\"><dl><dd>");
                IEnumerable<Model.SysManage.T_PUB_DICTIONARY> ModelListChildren = modelList.Where(p => p.PARENT_ID == item.ID);
                strHtml.Append(GetItem(ModelListChildren, item.Field));
                strHtml.Append(" </dd></dl></div></div>");
            }
            return strHtml.ToString();
        }

        private string GetItem(IEnumerable<Model.SysManage.T_PUB_DICTIONARY> ChildrenNode, string strField)
        {
            StringBuilder strResout = new StringBuilder();

            foreach (Model.SysManage.T_PUB_DICTIONARY item in ChildrenNode)
            {
                strResout.Append(string.Format(" <a target=\"_blank\" title=\"{0}\"", item.NAME));
                strResout.Append(string.Format("onclick=\"getSearchDate('{0}','{1}',this)\">{2}</a>", strField, item.ID, item.NAME));
            }
            return strResout.ToString(); ;
        }
    }
}